package com.pig4cloud.pig.djjk.service.immi;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.pig4cloud.pig.admin.api.util.ParamResolver;
import com.pig4cloud.pig.djjk.config.FaceApiConfig;
import com.pig4cloud.pig.djjk.domain.po.FaceLibSearchResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.logging.Logger;

@Service
public class FaceApiService {
    Logger log = Logger.getLogger(FaceApiService.class.getName());
    @Autowired
	FaceApiConfig faceApiConfig;

    public FaceLibSearchResp faceLibSearch(String photoBase64) {
        log.info("uav found!!! faceLibSearch request");
        if(!StrUtil.startWith(photoBase64,"data:image")){
            photoBase64 = "date:image/png;base64,"+photoBase64;
        }
        JSONObject request = new JSONObject();
        request.putOpt("searchImageBase64",photoBase64);

		String threshold = ParamResolver.getStr("FACE_THRESHOLD", "75");
		request.putOpt("score",Convert.toFloat(threshold)/100);
        String response = HttpUtil.post(faceApiConfig.getUrl()+"/border/api/faceLibSearch",request.toString());
        log.info("faceLibSearch response:"+response);
        if(StrUtil.isNotEmpty(response) && JSONUtil.isJson(response)){
            JSONObject result = JSONUtil.parseObj(response);
            if(result.getInt("code")==200){
                JSONObject data = result.getJSONObject("data");
                FaceLibSearchResp faceLibSearchResp =  JSONUtil.toBean(data,FaceLibSearchResp.class);
                return faceLibSearchResp;
            }
        }
        return null;
    }
}
